//
//  BLLeftViewController.m
//  BiologyLibrary
//
//  Created by milo on 16/7/1.
//  Copyright © 2016年 youxh. All rights reserved.
//

#import "BLLeftViewController.h"
#import "RESideMenu.h"
#import "CustomNavigationController.h"
#import "LoginViewController.h"
@interface BLLeftViewController ()

@end

@implementation BLLeftViewController
static NSString *g_strKey_Image = @"Image";
static NSString *g_strKey_Title = @"Title";
- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view.
    _contentArray = [NSArray arrayWithObjects:
                     [NSDictionary dictionaryWithObjectsAndKeys:
                      @"menua",g_strKey_Image,@"数据记录",g_strKey_Title, nil],
                     [NSDictionary dictionaryWithObjectsAndKeys:
                      @"menub",g_strKey_Image,@"账户信息",g_strKey_Title, nil],
//                     [NSDictionary dictionaryWithObjectsAndKeys:
//                      @"menub",g_strKey_Image,@"清除缓存",g_strKey_Title, nil],
//                     [NSDictionary dictionaryWithObjectsAndKeys:
//                      @"menuc",g_strKey_Image,@"修改密码",g_strKey_Title, nil],
//                     [NSDictionary dictionaryWithObjectsAndKeys:
//                      @"menud",g_strKey_Image,@"关于我们",g_strKey_Title, nil],
                     nil];
    
    
    int julihight = 0;
    _fHeadHeight = 178.0;
    float btnwidth = 170;
    float btnhidth = 45;
    float readious = 23;

    if (bIsiPhone6p) {
        julihight = 30;
        _fHeadHeight = 198.0;
        btnwidth = 210;
        btnhidth = 55;
        readious = 28;
    }else if (bIsiPhone6){
        julihight = 30;
        _fHeadHeight = 188.0;
        btnwidth =190;
        btnhidth = 50;
        readious =25;
    }
    
    [self.view addSubview:[self creatHeadView]];
    
    _contentTableView = [[UITableView alloc]initWithFrame:
                         CGRectMake(20, fHeight_StatueBar + _fHeadHeight+julihight, fWidth_Current,
                                    fHeight_Current - fHeight_StatueBar - _fHeadHeight-100)
                                                    style:0];
    _contentTableView.backgroundColor = [UIColor clearColor];
    _contentTableView.showsVerticalScrollIndicator = NO;
    _contentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    if (bIsiPhone6p) {
        _contentTableView.rowHeight = 65.0;

    }else if(bIsiPhone6){
        _contentTableView.rowHeight = 55.0;

    }else{
        _contentTableView.rowHeight = 45.0;

    }
    _contentTableView.dataSource = self;
    _contentTableView.delegate = self;
    [self.view addSubview:_contentTableView];
    
    UIButton *loginoutbtn=[[UIButton alloc]initWithFrame:CGRectMake(30, fHeight_Current-100, btnwidth, btnhidth)];
    loginoutbtn.backgroundColor=[UIColor clearColor];
    [loginoutbtn setTitle:@"退出" forState:UIControlStateNormal];
    loginoutbtn.titleLabel.font=[UIFont systemFontOfSize:16];
    
    [loginoutbtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    loginoutbtn.layer.borderWidth = 0.5;
    loginoutbtn.layer.cornerRadius=readious;
    loginoutbtn.clipsToBounds=YES;
    loginoutbtn.layer.borderColor = [[UIColor whiteColor] CGColor];
    [loginoutbtn addTarget:self action:@selector(outlogin) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:loginoutbtn];

}
-(void)outlogin{  //特别注意登出以后 数据的处理
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"确定退出登录？" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
    alert.delegate=self;
    alert.tag=1;
    [alert show];
    
    
}
#pragma mark UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertView.tag==1) {
        if (buttonIndex==1) {
            [APPDataCenter clearUserInfo];
            [self.view hideToastActivityWithSuccess:@"退出成功" completion:^{
                [self pushlogin];
            }];
        }
    }
}
-(void)pushlogin
{
    LoginViewController *viewController = [[LoginViewController alloc] init];
    CustomNavigationController *loginNavC = [[CustomNavigationController alloc] initWithRootViewController:viewController];
    
    [[[UIApplication sharedApplication] keyWindow] setRootViewController:loginNavC];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - UserAction
- (UIView *)creatHeadView{
    
    if (!_menuHeadImageView) {
        
        _menuHeadImageView = [[UIImageView alloc] initWithFrame:
                              CGRectMake(0.0, 0.0, self.view.frame.size.width * 0.7,
                                         _fHeadHeight)];
        _menuHeadImageView.userInteractionEnabled = YES;
        _menuHeadImageView.backgroundColor = [UIColor clearColor];
        float lab2size = 0.0;
        float lab1size = 0.0;
        float imgsize = 0.0;
        if (bIsiPhone6p) {
            lab2size = 17;
            lab1size = 22;
            imgsize=100.0;
        }else if(bIsiPhone6){
            lab2size = 15;
            lab1size = 20;
            imgsize=90.0;
            
        }else{
            imgsize=80.0;
            lab2size = 13;
            lab1size = 18;
            
            
        }

        _iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake((_menuHeadImageView.frame.size.width - imgsize) / 2,
                                                                       54.0, imgsize, imgsize)];
        _iconImageView.backgroundColor = [UIColor clearColor];
        _iconImageView.userInteractionEnabled = YES;
        _iconImageView.layer.cornerRadius=40;
        _iconImageView.clipsToBounds=YES;
        _iconImageView.image=[UIImage imageNamed:@"userhead_temp"];
        [_menuHeadImageView addSubview:_iconImageView];
        
        UITapGestureRecognizer  *tapHeadImage = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickHeadImage:)];
        [_iconImageView addGestureRecognizer:tapHeadImage];
        
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, imgsize+54+10, _menuHeadImageView.frame.size.width, 20.0)];
        _nameLabel.backgroundColor = [UIColor clearColor];
        _nameLabel.textAlignment = NSTextAlignmentCenter;
        _nameLabel.font = [UIFont systemFontOfSize:lab1size];
        _nameLabel.textColor = [UIColor whiteColor];
        _nameLabel.text=[APPUserDict objectForKey:@"displayname"];
        [_menuHeadImageView addSubview:_nameLabel];
        
        _jueseLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, imgsize+54+10+30, _menuHeadImageView.frame.size.width, 20.0)];
        _jueseLabel.backgroundColor = [UIColor clearColor];
        _jueseLabel.textAlignment = NSTextAlignmentCenter;
        _jueseLabel.font = [UIFont systemFontOfSize:lab2size];
        _jueseLabel.textColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.6];
        //收血研究生、技术员、样本库管理员、admin:0-3
        NSString*type=[APPUserDict objectForKey:@"usertype"];
        if ([type isEqualToString:@"0"]) {
            _jueseLabel.text=@"(收血研究生)";
        }else if([type isEqualToString:@"1"]){
            _jueseLabel.text=@"(技术员)";
        }else if([type isEqualToString:@"2"]){
            _jueseLabel.text=@"(样本库管理员)";
        }else if([type isEqualToString:@"3"]){
            _jueseLabel.text=@"(admin)";
        }else{
            _jueseLabel.text=@"(未知)";
        }
        [_menuHeadImageView addSubview:_jueseLabel];
    }
    
    return _menuHeadImageView;
}

-(void)clickHeadImage:(UIGestureRecognizer *)gr
{
//    MyHomeViewController *myHomeVC = [[MyHomeViewController alloc] init];
//    myHomeVC.iComeType = 1;
//    CustomNavigationController *myAllMatchNavC = [[CustomNavigationController alloc]
//                                                  initWithRootViewController:myHomeVC];
//    [self.sideMenuViewController setContentViewController:myAllMatchNavC animated:YES];
//    
//    [self.sideMenuViewController hideMenuViewController];
}

-(void)reSetUserHeadIamge{
    
    if ([APPDataCenter.userDict objectForKey:@"headpic"]) {
        
        [_iconImageView setImageWithURL:[NSURL URLWithString:[APPDataCenter.userDict objectForKey:@"headpic"]]
                       placeholderImage:[UIImage imageNamed:@"icon"]];
    }else{
        
        [_iconImageView setImage:[UIImage imageNamed:@"icon"]];
    }
}





#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [_contentArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *MenuCellIdentifier = @"MenuCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MenuCellIdentifier];
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:MenuCellIdentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleGray];
        
        if (bIsIOS7Later)
            cell.backgroundColor = [UIColor clearColor];
        
        cell.textLabel.backgroundColor = [UIColor clearColor];
        cell.textLabel.textColor = [UIColor whiteColor];
        float fHeight_Row = 0.0;
        float fwidth = 0.0;
        float imgsize = 0.0;

        if (bIsiPhone6p) {
            fHeight_Row = 65.0;
            fwidth = 200;
            cell.textLabel.font = [UIFont systemFontOfSize:20];
            imgsize=15.0;
        }else if(bIsiPhone6){
            fHeight_Row = 55.0;
            fwidth = 180;
            cell.textLabel.font = [UIFont systemFontOfSize:18];
            imgsize=13.0;

        }else{
            imgsize=10.0;
            fHeight_Row = 45.0;
            fwidth = 160;
            cell.textLabel.font = [UIFont systemFontOfSize:16];


        }
        UIImageView *rightview=[[UIImageView alloc]initWithFrame:CGRectMake(fwidth, (fHeight_Row-10)/2, imgsize, imgsize)];
        rightview.backgroundColor=[UIColor clearColor];
        rightview.image=[UIImage imageNamed:@"navleft"];
        [cell addSubview:rightview];
    }
    NSDictionary *tempDict = [_contentArray objectAtIndex:indexPath.row];
    NSArray*arr=[NSArray arrayWithObjects:@"leftviewtip0",@"leftviewtip1",@"leftviewtip2",@"leftviewtip3",@"leftviewtip4", nil];
//    cell.imageView.image = [UIImage imageNamed:[tempDict objectForKey:g_strKey_Image]];
    cell.imageView.image =[UIImage imageNamed:[arr objectAtIndex:indexPath.row]];
    cell.textLabel.text = [tempDict objectForKey:g_strKey_Title];
    
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    switch (indexPath.row) {
        case 0:{
            if (!_BlmainVC) {
                
                _BlmainVC = [[BLMainViewController alloc] init];
            }
            CustomNavigationController *BlmainNavC = [[CustomNavigationController alloc]
                                                         initWithRootViewController:_BlmainVC];
            [self.sideMenuViewController setContentViewController:BlmainNavC animated:YES];
            
            [self.sideMenuViewController hideMenuViewController];
            
        }
            break;
        case 1:{
            if (!_UserCenterVC) {
                
                _UserCenterVC = [[UserCenterViewController alloc] init];
            }
            CustomNavigationController *myFriendsNavC = [[CustomNavigationController alloc]
                                                         initWithRootViewController:_UserCenterVC];
            [self.sideMenuViewController setContentViewController:myFriendsNavC animated:YES];
            
            [self.sideMenuViewController hideMenuViewController];
            
        }
            break;
        case 2:{
            if (!_UserCenterVC) {
                
                _UserCenterVC = [[UserCenterViewController alloc] init];
            }
            CustomNavigationController *myFriendsNavC = [[CustomNavigationController alloc]
                                                         initWithRootViewController:_UserCenterVC];
            [self.sideMenuViewController setContentViewController:myFriendsNavC animated:YES];
            
            [self.sideMenuViewController hideMenuViewController];
            
        }
            break;
        case 3:{
            if (!_UserCenterVC) {
                
                _UserCenterVC = [[UserCenterViewController alloc] init];
            }
            CustomNavigationController *myFriendsNavC = [[CustomNavigationController alloc]
                                                         initWithRootViewController:_UserCenterVC];
            [self.sideMenuViewController setContentViewController:myFriendsNavC animated:YES];
            
            [self.sideMenuViewController hideMenuViewController];
        }
            break;
        case 4:{
            if (!_UserCenterVC) {
                
                _UserCenterVC = [[UserCenterViewController alloc] init];
            }
            CustomNavigationController *myFriendsNavC = [[CustomNavigationController alloc]
                                                         initWithRootViewController:_UserCenterVC];
            [self.sideMenuViewController setContentViewController:myFriendsNavC animated:YES];
            
            [self.sideMenuViewController hideMenuViewController];
           
        }
            break;
               default:
            break;
    }
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
